// pages/pay_page/pay_page.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay_way: [
      "微信支付",
      // "余额支付",
      // "货到付款"
    ],
    all_price: 0,
    order_list: [],
    address: null,
    order_dataStr: "",
    order_data: {},
    cart_num: 0,
    order_id: '',
    wran_text: '',
    other:{},
    freight:'',
    subtotal:'',
    difference:'',
    ispushing: false
  },
  get_text:function(e){
    this.remarks = e.detail.value
  },
  com_order: function () {
    this.setData({
      ispushing:true
    })
    var that = this
    var specval_id = []
    var specval = this.data.order_data.specval
    for (var i in specval) {
      specval_id.push(specval[i].specval_id + "_" + specval[i].num)
    }
    if (!this.data.address) {
      this.showtip('请选择地址')
      return
    }
    app.ajax({
      url: app.host + "mall/order/scheduling",
      success: function (res) {
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success: function (res){
              if (res.confirm){
                app.ajax({
                  url: app.host + "mall/order/add",
                  data: {
                    specval_id: specval_id.join(','),
                    address_id: that.data.address.address_id,
                    remarks: that.remarks?that.remarks:'',
                  },
                  success: function (res) {
                    var parameters = res.data.data.parameters
                    if (res.data.code > 0) {
                      that.setData({
                        order_id: parameters.order_id
                      })
                      parameters.success = function () {
                        app.ajax({
                          url: app.host + "mall/order/payok?order_id=" + parameters.order_id,
                          success: function () {
                            wx.redirectTo({
                              url: '/pages/msg/msg_success?order_id=' + parameters.order_id,
                            })
                          }
                        })
                      }
                      parameters.fail = function (err) {
                        wx.redirectTo({
                          url: '/pages/msg/msg_fail?order_id=' + parameters.order_id,
                        })
                      }
                      wx.requestPayment(parameters)
                    } else {
                      wx.showModal({
                        title: '温馨提示',
                        content: '服务器繁忙，请稍候再试，是否继续下单？',
                      })
                    }
                  }
                })
              }
            }
          })
        } else {
          app.ajax({
            url: app.host + "mall/order/add",
            data: {
              specval_id: specval_id.join(','),
              address_id: that.data.address.address_id,
              remarks: that.remarks ? that.remarks : '',
            },
            success: function (res) {
              var parameters = res.data.data.parameters
              if (res.data.code > 0) {
                that.setData({
                  order_id: parameters.order_id
                })
                parameters.success = function () {
                  app.ajax({
                    url: app.host + "mall/order/payok?order_id=" + parameters.order_id,
                    success: function () {
                      wx.redirectTo({
                        url: '/pages/msg/msg_success?order_id=' + parameters.order_id,
                      })
                    }
                  })
                }
                parameters.fail = function (err) {
                  wx.redirectTo({
                    url: '/pages/msg/msg_fail?order_id=' + parameters.order_id,
                  })
                }
                wx.requestPayment(parameters)
              } else {
                wx.showToast({
                  title: '服务器繁忙 请稍后再试',
                })
              }
            }
          })
        }
      }
    })

  },
  showtip: function (text) {
    var that = this
    this.setData({
      wran_text: text,
      showTopTips: true
    })
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 2000);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    let order_data = JSON.parse(options.order_data);
    console.log(order_data)
    var specval_id = []
    var specval = order_data.specval
    for (var i in specval) {
      specval_id.push(specval[i].specval_id)
    }
    this.setData({
      order_data: order_data,
      order_dataStr: JSON.stringify(order_data)
    })
    app.ajax({
      url: app.host + "mall/order/getgoods",
      data: {
        specval_id: specval_id.join(',')
      },
      success: function (res) {
        that.setData({
          all_price: order_data.allprice,
          order_list: res.data.data
        })
        app.ajax({
          url: app.host + "mall/order/orderother",
          success:function(res){
            var other = res.data.data
            console.log(other)
            var all_price = parseFloat(that.data.all_price)
            var delivery_fee = other.delivery_fee.delivery_fee
            var free_delivery_fee = other.delivery_fee.free_delivery_fee
            var freight = parseFloat(free_delivery_fee > 0 ? all_price >= free_delivery_fee ? '0.00' : delivery_fee : delivery_fee)
            var subtotal = parseFloat(freight) + parseFloat(all_price)
            var difference = -(all_price - free_delivery_fee)
            that.setData({
              freight: freight.toFixed(2),
              subtotal: subtotal.toFixed(2),
              difference: difference.toString(),
              other: other
            })
            console.log(that.data.difference)
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.ajax({
      url: app.host + "mall/user/address",
      success: function (res) {
        wx.hideLoading()
        that.setData({
          address: res.data.data[0] ? res.data.data[0] : null
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})